//
//  MainTopView.m
//  YKZB
//
//  Created by 曾少伟 on 2017/3/17.
//  Copyright © 2017年 曾少伟. All rights reserved.
//

#import "MainTopView.h"

@interface MainTopView ()
@property (nonatomic,strong)UIView *lineView;
@end

@implementation MainTopView

- (instancetype)initWithFrame:(CGRect)frame titlesNameArr:(NSArray *)titlesNameArr
{
    self = [super initWithFrame:frame];
    if (self) {
        for (NSInteger i = 0; i < titlesNameArr.count; i++) {
            CGFloat btnW = self.width/titlesNameArr.count;
            CGFloat btnH = self.height;
            
            UIButton *btn = [UIButton buttonWithTitle:titlesNameArr[i] titleColor:[UIColor whiteColor] font:18 backgroundColor:[UIColor clearColor] tagart:self action:@selector(btnAction:)];
            btn.tag = 100 + i;
            [self addSubview:btn];
            btn.frame = CGRectMake(i * btnW, 0, btnW, btnH);
            
            if (!self.lineView) {
                UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, btn.bottom + 3, btn.width * 0.8, 2)];
                lineView.backgroundColor = [UIColor whiteColor];
                [self addSubview:lineView];
                lineView.centerX = btn.centerX;
                self.lineView = lineView;
            }
        }
    }
    return self;
}

#pragma mark - event
- (void)btnAction:(UIButton *)sender {
    [self.zDelegate mainTopView:self clickBtn:sender.tag - 100];
    [self scrollToBtn:sender.tag - 100];
}

#pragma mark -M
- (void)scrollToBtn:(NSInteger)idx {
    UIButton *btn = [self viewWithTag:100 + idx];
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.centerX = btn.centerX;
    }];
}

@end
